<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//chan ip viet nam
class User extends CI_Controller {    
    private $content='';
    private $mensenger='';
    function  __construct(){
        parent::__construct();
        $this->load->model('Admin_model');
        $this->load->library('form_validation');
        
        //$this->inic->sysm();
        //$this->Admin_model->get_one('users',array('id'=>$this->session->userdata('user')->id));
        
    }
    function curlip($url){
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_URL,$url);
        $result = curl_exec($ch);  // grab URL and pass it to the variable.
        curl_close($ch);
        return $result;
    }
    function index(){
        if($this->session->userdata('logedin')){
            redirect('user/account');
        }else{
            redirect('user/login');
        }
    }
    function account(){
        if($this->session->userdata('logedin')){
        $this->content=$this->load->view('user/account',array('content'=>$this ->Admin_model->get_one('users',array('id'=>$this->session->userdata('user')->id))  ),true);
        $this->hienthi(); 
        }else{
            redirect('user/login');
        }      
        
    }
    function changepass(){
        if($this->session->userdata('logedin')){
            if($_POST){
                $this->form_validation->set_rules('oldpassword', 'Password cũ', 'trim|required|xss_clean');
                $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|min_length[5]|max_length[25]|xss_clean');
                $this->form_validation->set_rules('passconf', 'Confirm password', 'trim|required|xss_clean');            
                if ($this->form_validation->run() == TRUE){
                    $data = $this->security->xss_clean($_POST);// du lieu gui ve tu form
                    $password=sha1(md5($data['oldpassword']));
                    if($this->Home_model->get_number('users',array('id'=>$this->session->userdata('user')->id,'password'=>$password))==1){
                        $this->db->where('id',$this->session->userdata('user')->id);
                        $this->db->update('users',array('password'=>sha1(md5($data['password']))));
                        $this->session->set_userdata('doipass',1);
                    }else{
                        $this->session->set_userdata('loidoipass','Password không đúng vui lòng nhập lại');
                    }
                        
                    }                     
                }
               $this->content = $this->load->view('user/change_pass',array(),true);
               $this->hienthi();
        }else{
            redirect('user/login');
        } 
    }
    function payout()
    {
        if($this->session->userdata('logedin')){
            $this->session->unset_userdata('loi');
            $account = $this ->Admin_model->get_one('users',array('id'=>$this->session->userdata('user')->id));
            //username,status,user_id
            // wall ->1 \\\ banner-> 2            
            if(!empty($_POST)){
                $this->form_validation->set_rules('method', 'Method', 'trim|required|xss_clean');
                $this->form_validation->set_rules('point', 'Point', 'trim|numeric|required|xss_clean');
                $this->form_validation->set_rules('accpay', 'Paypal/ LR', 'trim|required|xss_clean');
                $data = $this->security->xss_clean($_POST);// du lieu gui ve tu form
                if ($this->form_validation->run() == TRUE){ 
                    if($account->curent < $this->config->item('mini_payout')){                    
                        $this->session->set_userdata('loi','<b style="color:red;">&nbsp;&nbsp;&nbsp;Sorry, but the minimum cashout balance at this time is 0.2$.</b>');
                    }else{                    
                        if($data['point']>=$account->curent){// check point cash voi point tai khoan neu no nhap lon hon thi cho bang
                            $data['point']=$account->curent;
                        }
                        $data['ip'] = $this->input->ip_address();
                        $data['status']='Pending';
                        $data['iduser'] = $account->id;
                        $this->db->insert('payout', $data); 
                        $point = $account->curent - $data['point'];
                        $this->db->where('id', $account->id);
                        $this->db->update('users', array('curent'=>$point)); 
                        $this->session->set_userdata('loi','Success!');
                    }
                }
            }            
            $this->content = $this->load->view('user/payout',
                array(
                    'pay'=>$this->Home_model->get_data('payout',array('iduser' => $account->id,'checked'=>0))
                ),true
            );
            $this->hienthi();         
        }else {            
            $this->content = 'Not login';
            $this->hienthi(); 
        }
        
    }
    function history(){
         if($this->session->userdata('logedin')){
            $userid = $this->session->userdata('user')->id;//username,status,user_id
           // wall ->1 \\\ banner-> 2
            $this->content =$this->load->view('user/history',array(
                                    'pay'=>$this->Home_model->get_data('payout',array('iduser' => $userid))  
                                    ),
            true);  
            $this->hienthi();         
        }else {
            $this->page = 'history';
            $this->content = 'Not login';
            $this->hienthi(); 
        }
        
    }
    function hthistory(){
         if($this->session->userdata('logedin')){
            // wall ->1 \\\ banner-> 2            
            $this->content =$this->load->view('user/hthistory',
                array(
                'pay'=>$this->Home_model->get_data('credit',array('iduser' => $this->session->userdata('user')->id))
                ),true
            );
             
            $this->hienthi();         
        }else {
            $this->page = 'hthistory';
            $this->content = 'Not login';
            $this->hienthi(); 
        }
        
    }
    function register($type=0,$fid=0){
        //get ip
        $ip =$this->input->ip_address();
        $url ="http://ipinfo.io/{$ip}";
        $details = json_decode($this->curlip($url));
        if(empty($details->country)){
            @$details->country= 'XX';
        }
        if($details->country=="VN"){
            $this->content= 'We don\'t allow your country!';
            $this->hienthi(); 
            return;
        }
        if($type=='group'){
            $groupid=$fid;
            $ref=0;
        }elseif($type=='ref'){
            $groupid=0;
            $ref=$fid;
        }else{
            $groupid=$ref=0;
        }
        if(!$this->session->userdata('logedin')){        
            $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[25]|xss_clean');
            $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|min_length[5]|max_length[25]|xss_clean');
            $this->form_validation->set_rules('passconf', 'Confirm password', 'trim|required|xss_clean');
            $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');
            $this->form_validation->set_rules('paypal', 'Paypal', 'trim|required|valid_email|xss_clean');        
             if ($this->form_validation->run() == FALSE){
            //  $this->session->set_userdata('loidn',1);
            }else{
                $data =$this->security->xss_clean($_POST);
                $data['mailling']= serialize($data['mailling']);
                $data['ip']=$this->input->ip_address();
                $data['password']=sha1(md5($data['password']));  
                $data['groupid'] =$groupid;  
                $data['ref'] =$ref;    
                $data['banned']=1;
                unset($data['passconf']); 
                unset($data['term']);           
                if(!$this->check_user($data['username'],$data['email'],$data['password'])){
                    $this->db->insert('users',$data); 
                    //chenn them vao bang stast
                    $this->db->where('id',1)->set('mem','mem + 1', false)->update('stast');
                    
                    $this->session->set_userdata('loidn','You have successfully registered!');
                    redirect('user');               
                }
                $this->session->set_userdata('loidk',$this->mensenger);
                
            }
            $this->content=$this->load->view('user/register','',true);
            @$data->content = $this->content;
            $dt = $this->template->render($data);  
            $this->load->view('default/viewfull',$dt);  
        }else{
            redirect('user/account');
        } 
        
    }    
    function login(){ 
        $ip =$this->input->ip_address();
        $url ="http://ipinfo.io/{$ip}";
        $details = json_decode($this->curlip($url));
        if(empty($details->country)){
            @$details->country= 'XX';
        }
        if($details->country=="VN"){
            $this->content= 'We don\'t allow your country!';
            $this->hienthi(); 
            return;
        }
       if(!$this->session->userdata('logedin')){
           $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
           $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');      
           if ($this->form_validation->run() == TRUE){
                $username = $this->security->xss_clean($_POST['username']);
                $password = $this->security->xss_clean($_POST['password']);
                $password = sha1(md5($password));
                $log = $this ->Admin_model->get_one('users',array('username'=>$username,'password'=>$password));
                $num = $this ->Admin_model->get_number('users',array('username'=>$username,'password'=>$password));
                //get_number   
                //0 la pending 1 la approved 3 la banned        
                if($num==1){ 
                   /// if($num->banned==1){
                        $this->session->set_userdata('logedin',1);
                        $this->session->set_userdata('user',$log); 
                        redirect('user/account');
                    //}else{
                      //  $this->session->set_userdata('loidn','Acount is pendding!');
                   // }
                                                   
                }else{                
                    $this->session->set_userdata('loidn','Incorrect login - <a href="'.base_url().'user/register">Register</a>');
                }
           }
                    
          $this->content=$this->load->view('user/login','',true);
            @$data->content = $this->content;
            $dt = $this->template->render($data);  
            $this->load->view('default/viewfull',$dt);  
        }else{
            redirect('user/account');
        }
    }
    function check_user($username,$email,$pass){        
        if($this->Admin_model->check_trung(array('username'=>$username),'users')||
        $this->Admin_model->check_trung(array('email'=>$email),'users') ){
            if($this->Admin_model->check_trung(array('username'=>$username),'users')){
                $this->mensenger.='Username already exists</br>';                
            }
            if($this->Admin_model->check_trung(array('email'=>$email),'users')){
                $this->mensenger.='Email already exists</br>';                
            }
            return true;// la loi
        }else{
            return false;
        }
    }
    function logout(){
        $this->session->unset_userdata('logedin');
        $this->session->unset_userdata('user'); 
        redirect(base_url());
    }
    function hienthi(){  
        @$data->content = $this->content;
        $dt = $this->template->render($data);  
        $this->load->view('default/twocol',$dt);        
       
    }
    
}